package com.yuyu.controller;

import com.yuyu.bean.Order;
import com.yuyu.bean.OrderItem;
import com.yuyu.service.OrderItemService;
import com.yuyu.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

@Controller
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderItemService orderItemService;

    //跳转到结账页面
    @RequestMapping("/toCollectMoneyPage")
    public String toCollectMoneyPage(){
        return "collect-money";
    }

    //跳转到账单信息页面
    @RequestMapping("/toBillPage")
    public String toBillPage(){
        return "bill";
    }

    //通过id查询订单
    @RequestMapping("/findOrderById")
    public String findOrderById(Integer id){
        Order order = orderService.findOrderById(id);
        System.out.println(order);
        return null;

    }



    //增加订单，并且返回订单对象
    @RequestMapping("/addOrderAndReturnOrder")
    @ResponseBody
    public Map addOrderAndReturnOrder(){

//        Integer lastId = orderService.getLastId();
//        Integer newId = lastId + 1;
        Order order = new Order();
        order.setName("订单");
        int result = orderService.addOrder(order);
        //先插入，再查最后一个插入
        Integer lastId = orderService.getLastId();
        order.setId(lastId);
        Map<String,Object> map = new HashMap<>();
        map.put("state_code",result);
        map.put("order",order);
        return map;
    }

    @RequestMapping("/toBill")
    public String toBill(Model model){
        Integer lastId = orderService.getLastId();
        List<OrderItem> orderItemList = orderItemService.findOrderItemWithOrderId(lastId);
        model.addAttribute("orderItemList",orderItemList);
        return "bill";
    }

    @RequestMapping("/confirmTheCheck")
    @ResponseBody
    public Map confirmTheCheck(Integer id,String cashier,Double amount_receivable,Double total_money){
        Order order = new Order();
        order.setId(id);
        order.setAmount_receivable(amount_receivable);
        order.setCashier(cashier);
        order.setTotal_money(total_money);
        order.setState(1);
        int result = orderService.editOrder(order);
        Map<String,Object> map = new HashMap<>();
        map.put("state_code",result);
        return map;
    }

}
